package com.example.app;

import android.content.ContentResolver;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.logging.LogRecord;

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    public static final int COMMAND_PLAY = 1;
    public static final int COMMAND_PAUSE = 2;
    public static final int COMMAND_NEXT = 3;
    public static final int COMMAND_PREVIOUS = 4;
    public static final int COMMAND_LED1 = 5;
    public static final int COMMAND_LED2 = 6;
    public static final int COMMAND_LED3 = 7;


    public static boolean PLAY = false;

    private TextView rcv;
    private EditText address;
    private Button connect;
    private Button singOrPause;
    private Button previous;
    private Button next;
    private Button above;
    private Button left;
    private Button right;
    private Socket socket;


   Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    Bundle bundle = msg.getData();

                    rcv.setText(bundle.getString("msg"));

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rcv = (TextView)findViewById(R.id.rev);
        address = (EditText)findViewById(R.id.address);
        singOrPause = (Button)findViewById(R.id.singOrPause);
        singOrPause.setOnClickListener(this);
        previous = (Button)findViewById(R.id.previous);
        previous.setOnClickListener(this);
        next = (Button)findViewById(R.id.next);
        next.setOnClickListener(this);
        above = (Button)findViewById(R.id.above);
        above.setOnClickListener(this);
        left = (Button)findViewById(R.id.left);
        left.setOnClickListener(this);
        right = (Button)findViewById(R.id.right);
        right.setOnClickListener(this);
        connect = (Button)findViewById(R.id.connect);
        connect.setOnClickListener(this);

        String text = address.getText().toString();
        address.setSelection(text.length());

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.connect:
                new ConnectThread().start();
                break;
            case R.id.singOrPause:
                if(!PLAY) {
                    PLAY = true;
                new SendThread(COMMAND_PLAY).start();
                } else {
                    new SendThread(COMMAND_PAUSE).start();
                    PLAY = false;
                }
                break;
            case R.id.previous:
                new SendThread(COMMAND_PREVIOUS).start();
                break;
            case R.id.next:
                new SendThread(COMMAND_NEXT).start();
                break;
            case R.id.above:
                new SendThread(COMMAND_LED3).start();
                break;
            case R.id.left:
                new SendThread(COMMAND_LED2).start();
                break;
            case R.id.right:
                new SendThread(COMMAND_LED1).start();
                break;


            default:
                break;
        }


    }

    class SendThread extends Thread{

        private int status;
        public SendThread(int status) {
            this.status = status;
        }

        @Override
        public void run() {
            try {


                OutputStream out;
                DataOutputStream dout;

                out = socket.getOutputStream();

                dout = new DataOutputStream(out);
                dout.writeInt(status);
                dout.flush();
            }catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

    class readThread extends Thread {


        @Override
        public void run() {
            Message msg = new Message();
            msg.what = 1;
            Bundle bundle = new Bundle();
            bundle.clear();

            try {

                DataInputStream in = new DataInputStream(socket.getInputStream());
                byte[] bytes = new byte[100];
                in.read(bytes);

            String line = new String(bytes);
            bundle.putString("msg",line);
            msg.setData(bundle);
            handler.sendMessage(msg);

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }


    class ConnectThread extends Thread {

        @Override
        public void run() {

            Message msg = new Message();
            msg.what = 1;
            Bundle bundle = new Bundle();
            bundle.clear();
            try {
                socket = new Socket();


                socket.connect(new InetSocketAddress(address.getText().toString(), 1234), 2000);

                new readThread().start();

            } catch (SocketTimeoutException a) {
                bundle.putString("msg", "服务器连接失败");
                msg.setData(bundle);
                handler.sendMessage(msg);

            }catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


}


